package com.sanxiau.dormitory.yang.service.impl;

import com.sanxiau.dormitory.yang.entity.*;
import com.sanxiau.dormitory.yang.face.Base64Util;
import com.sanxiau.dormitory.yang.face.FaceAdd;
import com.sanxiau.dormitory.yang.face.FaceDelete;
import com.sanxiau.dormitory.yang.mapper.*;
import com.sanxiau.dormitory.yang.service.Ex_dorService;
import com.sanxiau.dormitory.yang.service.StudentMessageService;
import com.sanxiau.dormitory.yang.vo.ResultInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class Ex_dorServiceImpl implements Ex_dorService {
    @Autowired
    Ex_dorMapper ex_dorMapper;
    @Autowired
    AttendanceMapper attendanceMapper;
    @Override
    public ResultInfo addEx_dor(Ex_dor ex_dor,HttpSession session) {
        ResultInfo resultInfo = new ResultInfo();
        Ex_dor ex_dor1 = ex_dorMapper.queryEx_dorByStu_id(session.getAttribute("userName")+"");
        if (null == ex_dor1) {
            if (null == ex_dor1) {
                Student student = attendanceMapper.queryStudentByStu_id(session.getAttribute("userName") + "");
                Riches riches = ex_dorMapper.queryRiches(ex_dor.getNew_hou_id(),ex_dor.getNew_dor_id(),ex_dor.getNew_bed_id());
                if (null == riches){
                    ex_dor.setStu_idea("同意");
                }else {
                    ex_dor.setAdv_id(riches.getStu_id());
                }
                ex_dor.setDate(new Date());
                ex_dor.setStu_study(student.getStu_study());
                ex_dor.setStu_class(student.getStu_class());
                ex_dor.setStu_cname(student.getStu_cname());
                int num = ex_dorMapper.addEx_dor(ex_dor);
                if (num > 0) {
                    resultInfo.setMessage("申请提交成功，请等待审批");
                } else {
                    resultInfo.setMessage("申请提交失败，请重试");
                }
            } else {
                resultInfo.setMessage("申请提交失败，请重试");
            }
        }else {
            resultInfo.setMessage("存在还未完成申请，提交失败");
        }
        return resultInfo;
    }

    @Override
    public List<String> showBed(String hou_id, String dor_id) {
        Dorm dorm = ex_dorMapper.queryDorm(hou_id,dor_id);
        List<String> beds = new ArrayList<>();
        if (null != dorm){
            for (int i = 1; i <= dorm.getDor_num(); i++) {
                beds.add(i+"");
            }
        }
        return beds;
    }

    @Override
    public List<Ex_dor> showEx_dor(HttpSession session) {
        LoginIn loginIn = attendanceMapper.queryUserByUser_name(session.getAttribute("userName")+"");
        List<Ex_dor> ex_dors = new ArrayList<>();
        if (null != loginIn){
            if ("学生".equals(loginIn.getRemark())){
                ex_dors = ex_dorMapper.queryEx_dor(loginIn.getUser_name());
            }else {
                ex_dors = ex_dorMapper.queryAllEx_dors();
            }
        }
        return ex_dors;
    }

    @Transactional
    @Override
    public ResultInfo passEx_dors(int[] ids, HttpSession session) {
        ResultInfo resultInfo = new ResultInfo();
        int count = 0;
        int sum = 1;
        Ex_dor ex_dor = new Ex_dor();
        LoginIn loginIn = attendanceMapper.queryUserByUser_name(session.getAttribute("userName")+"");
        if (null != loginIn){
            if ("学生".equals(loginIn.getRemark())){
                ex_dor.setStu_idea("同意");
            }else if ("宿管".equals(loginIn.getRemark())){
                ex_dor.setStu_idea("同意");
                ex_dor.setDor_idea("同意");
            }else {
                ex_dor.setStu_idea("同意");
                ex_dor.setDor_idea("同意");
                ex_dor.setIns_idea("同意");
                ex_dor.setApproval_date(new Date());
                ex_dor.setResult("同意");

            }
            for (int i = 0; i < ids.length; i++) {
                ex_dor.setId(ids[i]);
                if ("同意".equals(ex_dor.getResult())) {
                    Ex_dor ex_dor1 = ex_dorMapper.queryEx_dorByID(ids[i]);
                    if (null == ex_dor1.getAdv_id() || "".equals(ex_dor1)) {
                        Dorm dorm = ex_dorMapper.queryDorm(ex_dor1.getNew_hou_id(), ex_dor1.getNew_dor_id());
                        dorm.setDor_price(dorm.getDor_price()+1);
                        int num1 = ex_dorMapper.updateDorm(dorm);
                        House house = ex_dorMapper.queryHouse(ex_dor1.getNew_hou_id());
                        int num2 = ex_dorMapper.updateHouse(ex_dor.getNew_hou_id(),house.getHou_fact()+1);
                        Dorm dorm1 = ex_dorMapper.queryDorm(ex_dor1.getOld_hou_id(), ex_dor1.getOld_dor_id());
                        dorm1.setDor_price(dorm1.getDor_price()-1);
                        int num3 = ex_dorMapper.updateDorm(dorm1);
                        House house1 = ex_dorMapper.queryHouse(ex_dor1.getOld_hou_id());
                        int num4 = ex_dorMapper.updateHouse(ex_dor.getOld_hou_id(),house1.getHou_fact()-1);
                        if ((num1 * num2 * num3 * num4) >0){
                            sum = 1;
                        }else {
                            sum = 0;
                        }
                    }else {
                        Riches riches = new Riches();
                        riches.setStu_id(ex_dor1.getStu_id());
                        riches.setHou_id(ex_dor1.getNew_hou_id());
                        riches.setDor_id(ex_dor1.getNew_dor_id());
                        riches.setBed_id(ex_dor1.getNew_bed_id());
                        int num5 = ex_dorMapper.updateRiches(riches);
                        riches.setStu_id(ex_dor1.getAdv_id());
                        riches.setHou_id(ex_dor1.getOld_hou_id());
                        riches.setDor_id(ex_dor1.getOld_dor_id());
                        riches.setBed_id(ex_dor1.getOld_bed_id());
                        int num6 = ex_dorMapper.updateRiches(riches);
                        int num7 = ex_dorMapper.updateStudent(ex_dor1.getNew_hou_id(), ex_dor1.getNew_dor_id(), ex_dor1.getStu_id());
                        int num8 = ex_dorMapper.updateStudent(ex_dor1.getOld_hou_id(), ex_dor1.getOld_dor_id(), ex_dor1.getAdv_id());
                        if ((num5 * num6 * num7 * num8) >0){
                            sum = 1;
                        }else {
                            sum = 0;
                        }
                    }
                }
                int num = ex_dorMapper.updateEx_dor(ex_dor);
                if ((num*sum) > 0){
                    count++;
                }
            }
        }else {
            resultInfo.setMessage("操作出现错误，请重试");
        }
        if (count == ids.length){
            resultInfo.setMessage("操作成功");
        }else {
            resultInfo.setMessage("操作出现错误，请重试");
            throw new RuntimeException("操作出现错误，请重试");
        }
        return resultInfo;
    }

    @Override
    public ResultInfo overEx_dors(int[] ids, HttpSession session) {
        ResultInfo resultInfo = new ResultInfo();
        int count = 0;
        Ex_dor ex_dor = new Ex_dor();
        LoginIn loginIn = attendanceMapper.queryUserByUser_name(session.getAttribute("userName")+"");
        if (null != loginIn){
            if ("学生".equals(loginIn.getRemark())){
                ex_dor.setStu_idea("拒绝");
                ex_dor.setApproval_date(new Date());
                ex_dor.setResult("拒绝");
            }else if ("宿管".equals(loginIn.getRemark())){
                ex_dor.setStu_idea("同意");
                ex_dor.setDor_idea("拒绝");
                ex_dor.setApproval_date(new Date());
                ex_dor.setResult("拒绝");
            }else {
                ex_dor.setStu_idea("同意");
                ex_dor.setDor_idea("同意");
                ex_dor.setIns_idea("拒绝");
                ex_dor.setApproval_date(new Date());
                ex_dor.setResult("拒绝");
            }
            for (int i = 0; i < ids.length; i++) {
                ex_dor.setId(ids[i]);
                int num = ex_dorMapper.updateEx_dor(ex_dor);
                if (num > 0){
                    count++;
                }
            }
        }else {
            resultInfo.setMessage("操作出现错误，请重试");
        }
        if (count == ids.length){
            resultInfo.setMessage("操作成功");
        }else {
            resultInfo.setMessage("操作出现错误，请重试");
        }
        return resultInfo;
    }
}
